package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.config.AccPushProccesor;
import com.example.prison.config.ApiUtil;
import com.example.prison.entity.SecurityDeviceInfo;
import com.example.prison.mapper.SecurityDeviceInfoMapper;
import com.example.prison.service.SecurityDeviceInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


/**
* @author jiang
* @description 针对表【security_device_info】的数据库操作Service实现
* @createDate 2023-04-19 11:31:11
*/
@Service
public class SecurityDeviceInfoServiceImpl extends ServiceImpl<SecurityDeviceInfoMapper, SecurityDeviceInfo>
    implements SecurityDeviceInfoService {

    @Autowired
    private SecurityDeviceInfoMapper securityDeviceInfoMapper;

    @Override
    public PageInfo<SecurityDeviceInfo> get(int pageNumber, int pageSize, String deviceName, String deviceNumber) {
        List<SecurityDeviceInfo> list = securityDeviceInfoMapper.get(deviceName, deviceNumber);
        String time = ApiUtil.timeProcess(-2);
        for(SecurityDeviceInfo device : list) {
//            String status = device.getDeviceStatus();
//            if(AccPushProccesor.number == null || AccPushProccesor.time.compareTo(time) < 0) {
//                status = "离线";
//            } else if(Objects.equals(device.getDeviceNumber(), AccPushProccesor.number) && AccPushProccesor.time.compareTo(time) > 0) {
//                status = "在线";
//            }
            String status = ApiUtil.getDeviceStatus(device.getDeviceNumber());
            device.setDeviceStatus(status);
            securityDeviceInfoMapper.updateSecurityDevice(device, device.getDeviceId());
        }
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(securityDeviceInfoMapper.get(deviceName, deviceNumber));
    }

    @Override
    public int add(SecurityDeviceInfo securityDevice) {
        String deviceNumber = securityDevice.getDeviceNumber();
        if (ObjectUtils.isNotEmpty(securityDeviceInfoMapper.getOneByNumber(deviceNumber))) {
            return -1;
        } else {
            securityDevice.setDeviceStatus("离线");
            securityDeviceInfoMapper.add(securityDevice);
            return 1;
        }
    }

    @Override
    public void delete(Integer[] ids) {
        securityDeviceInfoMapper.deleteSecurityDevice(ids);
    }

    @Override
    public int update(SecurityDeviceInfo securityDevice, Integer deviceId) {
        securityDeviceInfoMapper.updateSecurityDevice(securityDevice, deviceId);
        return 1;
    }

    @Override
    public SecurityDeviceInfo getById(Integer deviceId) {
        return securityDeviceInfoMapper.getById(deviceId);
    }

    @Override
    public PageInfo<SecurityDeviceInfo> getByName(int pageNumber, int pageSize, String deviceName) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(securityDeviceInfoMapper.getByName(deviceName));
    }

    @Override
    public PageInfo<SecurityDeviceInfo> getByNumber(int pageNumber, int pageSize, String deviceNumber) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(securityDeviceInfoMapper.getByNumber(deviceNumber));
    }

    @Override
    public SecurityDeviceInfo getOneByNumber(String deviceNumber) {
        return securityDeviceInfoMapper.getOneByNumber(deviceNumber);
    }

    @Override
    public PageInfo<SecurityDeviceInfo> getStatus(int pageNumber, int pageSize) {
        List<SecurityDeviceInfo> list = securityDeviceInfoMapper.get(null,null);
        String time = ApiUtil.timeProcess(-2);
        for(SecurityDeviceInfo device : list) {
            String status = device.getDeviceStatus();
            if(Objects.equals(device.getDeviceNumber(), AccPushProccesor.number) && AccPushProccesor.time.compareTo(time) > 0) {
                status = "在线";
            }
            device.setDeviceStatus(status);
            securityDeviceInfoMapper.updateSecurityDevice(device, device.getDeviceId());
        }
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(securityDeviceInfoMapper.get(null,null));
    }

}




